<template>
    <div>
        <!--model:表单数据-->
        <el-form label-width="80px" :model="roleForm" :rules="rules" ref="formref">
            <!--            <el-form-item label="编号">-->
            <!--                <el-input v-model="roleForm.roleId" ></el-input>-->
            <!--            </el-form-item>-->
            <el-form-item label="角色名称" prop="roleName">
                <el-input v-model="roleForm.roleName"></el-input>
            </el-form-item>
            <el-form-item label="角色权限字符串" prop="roleKey">
                <el-input v-model="roleForm.roleKey"></el-input>
            </el-form-item>
            <el-form-item label="显示顺序" prop="sort">
                <el-input v-model="roleForm.sort"></el-input>
            </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
                <el-button @click="changestatus()">取 消</el-button>
                <el-button type="primary" @click=" confirmEditrole()">确 定</el-button>
        </span>
    </div>
</template>

<script>
    export default {
        name: "RoleAddOrupdata",
        props: ["roleId", "status"],
        model: {//绑定到父组件中
            prop: 'status',
            event: 'changestust'
        },
        data() {
            var Id = this.roleId
            var same = {id: '', same: ''}
            var that = this
            //唯一性校验
            var validatorrolename = function (rule, value, callback) {
                same.id = Id;
                same.same = value;
                that.$http.post(`/system/role/onlyonecheck/byname`, same).then(function (response) {
                    console.log(response.data)
                    if (response.data.result.type) {
                        callback();
                    } else {
                        callback("角色重复");
                    }
                });
            }
            return {
                roleForm: {},
                same: {id: '', name: ''},
                rules: {
                    roleName: [
                        {required: true, message: '菜单不为空', trigger: 'blur'},
                        {min: 2, message: '最少两个字符', trigger: 'blur'},
                        {validator: validatorrolename, trigger: 'blur'},

                    ],
                    roleKey: [
                        {required: true, message: '菜单不为空', trigger: 'blur'},
                        {min: 3, max: 6, message: '长度在 3 到 5 个字符', trigger: 'blur'},
                    ]
                }
            }
        },
        created() {
            this.getroleinfo();
        },
        methods: {
            //关闭窗口
            closewindow() {
                this.$emit("changestust", !this.status)
            },
            changestatus() {
                this.closewindow()
            },
            confirmEditrole() {
                var that = this
                this.$http.post('system/role/addorudate', this.roleForm).then(function (response) {
                    that.$message.success(response.data.message);
                })
                this.closewindow()
                location.reload()
            },
            getroleinfo() {
                if (this.roleId == '' || this.roleId == undefined) {

                } else {
                    var that = this;
                    var roleId = this.roleId;
                    this.$http.get(`system/role/getrolebyid/${this.roleId}`).then(function (response) {
                        that.roleForm = response.data.result.roleinfo;
                    });
                }

            }
        }
    }
</script>

<style scoped>

</style>